import re
import time
import jieba
from collections import defaultdict
from operator import itemgetter
from json import dump

start = time.time()

with open('WordCloud/src.txt', 'r', encoding='utf-8') as fr:
    txt = fr.read()

pattern = re.compile(
    '\d+|幢|号|室|(?:[,|，].*)|(?:、.*)|(?:[（|(].*[）|)])|(?:-)|(?:车库.*)')
result = re.sub(pattern, '', txt)

words = jieba.lcut(result)

counts = defaultdict(int)
for word in words:
    counts[word] += 1

counts = dict(sorted(counts.items(), key=itemgetter(1), reverse=True))

with open('WordCloud/result.json', 'w', encoding='utf-8') as fw:
    dump(counts, fw, ensure_ascii=False)

print(f'Time: {time.time() - start}')
